#include "Logger/tri_logger.hpp"
#include "MainWindow.h"
#include <QDockWidget.h>
#include <QTextEdit.h>
#include <QMenu.h>
#include <QMenuBar>
#include <QAction>
#include <QToolBar>
#include <QFontComboBox>
#include <QComboBox>
#include <QFontDatabase>
#include "Splitter.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
	TRI_LOG_STR("Gui Window!");
	splitter = new Splitter(this);
	setCentralWidget(splitter);

	createActions();
	createMenu();
	createWidgetDockFiles();
	createWidgetDockAlignment();
}

void MainWindow::setDocumentManager(DocumentManager* manager){
	document_manager = manager;
	splitter->init();
}

void MainWindow::createActions()
{

	/*
	 * creating actions
	 */
	QActionGroup* group_alignment = new QActionGroup(this);
	action_text_bold = new QAction(QIcon("Icon/32x32/actions/format-text-bold.png"),tr("&Bold"),NULL);
	action_text_underline = new QAction(QIcon("Icon/32x32/actions/format-text-underline.png"),tr("&Underline"),NULL);
	action_text_italic = new QAction(QIcon("Icon/32x32/actions/format-text-italic.png"),tr("&Italic"),NULL);
	action_align_left = new QAction(QIcon("Icon/32x32/actions/format-justify-left.png"),tr("&AlignmentLeft"),group_alignment);
	action_align_center = new QAction(QIcon("Icon/32x32/actions/format-justify-center.png"),tr("&AlignmentCenter"),group_alignment);
	action_align_right = new QAction(QIcon("Icon/32x32/actions/format-justify-right.png"),tr("&AlignmentRight"),group_alignment);	
	action_new  = new QAction(QIcon("Icon/32x32/actions/document-new.png"),tr("&New"),NULL);
	action_open = new QAction(QIcon("Icon/32x32/actions/document-open.png"),tr("&Open"),NULL);
	action_save = new QAction(QIcon("Icon/32x32/actions/document-save.png"),tr("&Save"),NULL);
	//SAVE-AS
	action_print= new QAction(QIcon("Icon/32x32/actions/document-print.png"),tr("&Print"),NULL);
	action_undo = new QAction(QIcon("Icon/32x32/actions/edit-undo.png"),tr("&Undo"),NULL);
	action_redo = new QAction(QIcon("Icon/32x32/actions/edit-redo.png"),tr("&Redo"),NULL);
	action_cut  = new QAction(QIcon("Icon/32x32/actions/edit-cut.png"),tr("&Cut"),NULL);
	action_copy = new QAction(QIcon("Icon/32x32/actions/edit-copy.png"),tr("&Copy"),NULL);
	action_paste= new QAction(QIcon("Icon/32x32/actions/edit-paste.png"),tr("&Paste"),NULL);
	
	/*
	 * adding keybord-shortcuts
	 */
	action_align_left->setShortcut(Qt::CTRL + Qt::Key_L);
	action_align_center->setShortcut(Qt::CTRL + Qt::Key_E);
	action_align_right->setShortcut(Qt::CTRL + Qt::Key_R);

	
	/*
	 * Making some actions checkable
	 */
	action_text_bold->setCheckable(true);
	action_text_underline->setCheckable(true);
	action_text_italic->setCheckable(true);
	action_align_left->setCheckable(true);
	action_align_center->setCheckable(true);
	action_align_right->setCheckable(true);

	connect(action_text_bold, SIGNAL(triggered()), splitter, SLOT(setBold()));
	connect(action_text_underline, SIGNAL(triggered()), splitter, SLOT(setUnderline()));
	connect(action_text_italic, SIGNAL(triggered()), splitter, SLOT(setItalic()));
	connect(group_alignment, SIGNAL(triggered(QAction *)), splitter, SLOT(setAlign(QAction *)));

	
}

void MainWindow::createMenu(){
    QMenu *menu = new QMenu(tr("&File"), this);
    menuBar()->addMenu(menu);

	
}
void MainWindow::createWidgetDockFiles(){
	QToolBar *tb = new QToolBar(this);
	tb->addAction(action_new);
	tb->addAction(action_open);
	tb->addAction(action_save);
	tb->addSeparator ();
	tb->addAction(action_print);
	tb->addSeparator ();
	tb->addAction(action_undo);
	tb->addAction(action_redo);
	tb->addSeparator ();
	tb->addAction(action_cut);
	tb->addAction(action_copy);
	tb->addAction(action_paste);
	tb->addSeparator ();

    addToolBar(tb);
	
}
void MainWindow::createWidgetDockAlignment(){
	QToolBar *tb = new QToolBar(this);
	font_combobox = new QFontComboBox(tb);
	size_combobox = new QComboBox(tb);
//	size_combobox->setObjectName("size_combobox");
	tb->addWidget(font_combobox);
	tb->addSeparator ();
	tb->addWidget(size_combobox);
	tb->addSeparator ();
	tb->addAction(action_text_bold);
	tb->addAction(action_text_underline);
	tb->addAction(action_text_italic);
	tb->addSeparator ();
	tb->addAction(action_align_left);
	tb->addAction(action_align_center);
	tb->addAction(action_align_right);
	tb->addSeparator ();
	tb->setWindowTitle(tr("Font Actions"));
	
    QFontDatabase db;
    foreach(int size, db.standardSizes())
    size_combobox->addItem(QString::number(size));
    size_combobox->setEditable(true);
    connect(size_combobox, SIGNAL(activated(const QString &)),
    		splitter, SLOT(setTextSize(const QString &)));

    connect(font_combobox, SIGNAL(activated(const QString &)),
    		splitter, SLOT(setTextFamily(const QString &)));
    
    addToolBar(tb);
	
}
void MainWindow::fontChanged(const QFont &f){
	font_combobox->setCurrentIndex(font_combobox->findText(QFontInfo(f).family()));
	size_combobox->setCurrentIndex(size_combobox->findText(QString::number(f.pointSize())));
	action_text_bold->setChecked(f.bold());
	action_text_italic->setChecked(f.italic());
    action_text_underline->setChecked(f.underline());
}

void MainWindow::alignmentChanged(Qt::Alignment a){
    if (a == Qt::AlignLeft) {
    	action_align_left->setChecked(true);
    } else if (a == Qt::AlignHCenter) {
    	action_align_center->setChecked(true);
    } else if (a == Qt::AlignRight){
    	action_align_right->setChecked(true);
    }
}

void MainWindow::fileNew(){
	
	
}
void MainWindow::fileOpen(){
	
	
}
bool MainWindow::fileSave(){
	return true;
}

